<?php
namespace plugins\order\model;
use think\Model;
use think\Db;

class OrderLogModel extends Model
{
	//订单状态转换
	public function getOrderStatusChinaAttr($value,$data)
	{
		$order_status = [1=>'待付款',2=>'待发货',3=>'已发货',4=>'已完成',5=>'已关闭'];
		return $order_status[$data['order_status']];
	}

	//付款状态转换
	public function getPayStatusChinaAttr($value,$data)
	{
		$pay_status = [1=>'未付款',2=>'已付款'];
		return $pay_status[$data['pay_status']];
	}

	//发货状态转换
	public function getDeliverStatusChinaAttr($value,$data)
	{
		$deliver_status = [1=>'未发货',2=>'已发货'];
		return $deliver_status[$data['deliver_status']];
	}

	//时间戳转换
	public function getLogTimeAttr($value)
	{
		$log_time = date('Y-m-d H:i:s',(int)$value);
		return $log_time;
	}

	//操作类型转换
	public function getLogTypeChinaAttr($value,$data)
	{
		$log_type = [10=>'创建订单',11=>'订单付款',12=>'订单发货',13=>'确认收货',14=>'用户评价',20=>'修改商品信息',21=>'修改收货人信息',22=>'修改费用信息',31=>'备注订单',32=>'关闭订单',33=>'取消订单',34=>'发送站内信'];
		return $log_type[$data['log_type']];
	}

	//创建编辑日志
	public function createLog($key,$order_num,$remark=null,$user=null)
	{
		$info = ( new \plugins\order\model\OrderLogModel )->field('order_num,order_status,pay_status,deliver_status')->where('order_num',$order_num)->limit(1)->order('log_time','desc')->find();
		$info->log_type = $key;
		$info->log_time = time();
		$info->remark = $remark;
		//操作人
		if($user == null){
			$uid = cmf_get_current_admin_id();
			$user = db('user')->field('user_login')->where(['id'=>$uid])->find();		
			$info->log_user = $user['user_login'];			
		}else if($user == 1){
			$info->log_user = '用户';
		}else if($user == 2){
			$info->log_user = '系统';
		}

		//订单付款,改变付款状态=>2,订单状态=>2
		if($key == 11){
			$info->order_status = 2;
			$info->pay_status = 2;
		}

		//订单发货,改变发货状态=>2,订单状态=>3
		if($key == 12 ){
			$info->order_status = 3;
			$info->deliver_status = 2;
		}

		//确认收货,订单状态=>4
		if($key == 13){
			$info->order_status = 4;
		}
		// dump($info);
		$return = $info->isUpdate(false)->save();
		if($return){
			return true;
		}else{
			return false;
		}
	}

}